<?php

namespace App\Http\Controllers\Api\User;

use App\Http\Controllers\Controller;
use App\Models\UserContactRecruit;
use App\Services\ContactService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class ContactController extends Controller
{
    public function list(Request $request)
    {
        $rule = [
            'user_id' => 'required'
        ];
        $input       = $request->input();
        $validateRet = Validator::make($input, $rule);
        if ($validateRet->fails()) {
            return result(1, $validateRet->errors()->first());
        }

        $page  = $request->input('page', 1);
        $limit = $request->input('limit', 10);
        $sortField  = $request->input('sort', 'user_contact_recruit.created_at');
        $orderRule = $request->input('order', 'desc');

        $userTableColumns = ['id', 'nickname', 'avatar'];
        $verifyTableColumns = ['id', 'user_id', 'type'];
        $recruitTableColumns = ['recruit.id', 'recruit.city', 'recruit.title', 'recruit.created_at', 'recruit.user_id', 'recruit.contact_phone'];

        try {
            $data = (new ContactService())->contactRecruitList($input, $page, $limit, $sortField, $orderRule,
                $userTableColumns, $verifyTableColumns, $recruitTableColumns);
        } catch (\Exception $e) {
            return result(1, $e->getMessage());
        }

        return result(0, 'ok', $data);
    }

    public function recordContact(Request $request)
    {
        $rule = [
            'user_id'    => 'required|integer',
            'recruit_id' => 'required|integer'
        ];
        $input       = $request->input();
        $validateRet = Validator::make($input, $rule);
        if ($validateRet->fails()) {
            return result(1, $validateRet->errors()->first());
        }

        $input['contact_time'] = date("Y-m-d", time());

        try {
            UserContactRecruit::query()->insert($input);
        } catch (\Exception $e) {
            return result(1, $e->getMessage());
        }

        return result(0, 'ok');
    }
}
